package JVM.philosper;

public class volatileTest {
    public static volatile int o=0;

    public static void main(String[] args) throws InterruptedException {
        Object object=new Object();
        Thread t1=new Thread(()->{
            synchronized (object) {
                for (int i = 1; i <= 10000; i++) {
                    o++;
                }
            }
        });

        Thread t2=new Thread(()->{
            //加上了syn，进行了同步，结果为20000
            synchronized (object) {
                for (int i = 1; i <= 10000; i++) {
                    o++;
                }
            }
        });
        t1.start();;
        t2.start();
        t1.join();
        t2.join();
        System.out.println(o); //输入的并不是20000，证明了volatile不能保证原子性，实际上 i++这个操作 是不具备原子醒的（底层应该分三步实现？）
    }

}
